package com.seed.core.fromaction.service.impl;

import com.seed.core.fromaction.service.FromActionServiceI;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author gchiaway
 * 日期: 2020-08-06
 * 时间: 17:01
 */
@Service
public class FromActionServiceImpl implements FromActionServiceI {
    /**
     * 锁变量
     */
    private final String FROM_ACTION_TOKEN_LOCK = "FROM_ACTION_TOKEN_LOCK";
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 校验请求令牌
     *
     * @param fromActionToken 请求令牌
     * @return 是否有效
     */
    @Override
    public Boolean checkFromActionToken(String fromActionToken) {
        if (StringUtils.isEmpty(fromActionToken)) {
            return false;
        }
        if (!redisUtil.hasKey(fromActionToken)) {
            return false;
        }
        synchronized (FROM_ACTION_TOKEN_LOCK) {
            if (!redisUtil.hasKey(fromActionToken)) {
                return false;
            }
            redisUtil.del(fromActionToken);
            return true;
        }
    }
}
